/************************************************************************
Author:			Trey Turley, Thomas Lee, Marc Willis

Filename:		DriverDraw.cpp

Classname:		Plotter

Description:	The plotter class allows us to plot characters
				to the screen. It uses a coordinate system to
				determine where the character will be placed.

Date Modified:	8/8/10
				- Menus were removed from this file and
				  put into there own.				
				8/7/10
				- Converted from test driver to working game
				8/3/10
				- File created
*************************************************************************/


const int SLEEPTIME=75;

const int TITLEX=26;
const int TITLEY=4;
const char TITLECHAR=218;

#include <iostream>
using namespace std;
#include "HPlayer.h"
#include "AIPlayer.h"
#include "Menu.h"
#include <strsafe.h>

int main()
{
	static int p1Wins=0,p2Wins=0,p3Wins=0,p4Wins=0;
	GameMap *game;
	Menu m;	
	int numPlayers=0;
	bool startGame=false, openMenu=false, customMap=false;
	Plotter scrn;

//resizing console window
	// Old code
	//HWND console = GetConsoleWindow();
	//RECT r;
	

	HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo;
	COORD dwSize;

	if (!GetConsoleScreenBufferInfo(hConsoleOutput, &lpConsoleScreenBufferInfo) ) {
		cout << "failed to get handle";
		throw;
	}

	dwSize.Y = 48;
	dwSize.X = 80;

	if (!SetConsoleScreenBufferSize(hConsoleOutput, dwSize) )
	{
		cout << "failed to set console buffer";
		throw;
	}

	lpConsoleScreenBufferInfo.srWindow.Left = 0;
	lpConsoleScreenBufferInfo.srWindow.Top = 0;
	lpConsoleScreenBufferInfo.srWindow.Bottom = 47;
	lpConsoleScreenBufferInfo.srWindow.Right = 79;

	if (!SetConsoleWindowInfo(hConsoleOutput, TRUE, &lpConsoleScreenBufferInfo.srWindow ) )
	{
		cout << "failed to set console window";
		throw;
	}
	TCHAR szOldTitle[MAX_PATH];
	TCHAR szNewTitle[MAX_PATH];

	if( GetConsoleTitle(szOldTitle, MAX_PATH) )
	{
		StringCchPrintf(szNewTitle, MAX_PATH, TEXT("                                                                                              TRON"), szOldTitle);
		if( !SetConsoleTitle(szNewTitle) )
		printf("SetConsoleTitle failed (%d)\n", GetLastError());
   }

	// Old code for console properties
	//GetWindowRect(console, &r); //stores the console's current dimensions
	//MoveWindow(console, r.left, r.top, 800, 600, TRUE);

//Display Title Screen
	m.title();	

	bool running=true;
	while(running){

	scrn.clear();

//Display player select menu
	numPlayers=m.plyrSlct();		
	scrn.clear();

//Display Player Controls
	m.plyrCtrl(numPlayers);		
		
//clear screen
	scrn.clear();




//Menu for map selection			
	if(m.mapSelect())
	{
		scrn.clear();
		scrn.setColor(white);
		scrn.move(0,0);
		ifstream infile;
		string name;
		cout<<"Enter the filename of the custom map\n(default maps: demomap.ted, finesse.ted)\n";
		cin>>name;
		//cin.ignore(1000,'\n');
		infile.open(name.c_str());

		while(!infile)
		{
			infile.clear();
			cout<<"That file does not exist"<<endl;
			cout<<"Enter a different filename"<<endl;
			cin>>name;	
			cin.ignore(1000,'\n');
			infile.open(name.c_str());		
		}	
		
	//	cout<<"Press enter to continue";
	//	cin.ignore(1000, '\n');

		game=new GameMap(infile,scrn);
	}else{
		game=new GameMap(80,41,scrn);
	}





//Start game message
	m.startGame();

//clear screen
	scrn.clear();

//draw the game map
	//scrn.drawMap();

//begin game preperations
	

		
		game->drawMap();
		HPlayer unPlayer(0,0,0,0,*game);
		unPlayer.setAlive(false);
		Player *p1,*p2,*p3,*p4;
		if(numPlayers==0)
		{
			p1=new AIPlayer(game->getPD(0,0),game->getPD(0,1),game->getPD(0,2),blue,*game,NULL);
			p2=new AIPlayer(game->getPD(1,0),game->getPD(1,1),game->getPD(1,2),red,*game,p1);
			p3=new AIPlayer(game->getPD(2,0),game->getPD(2,1),game->getPD(2,2),green,*game,p1);
			p4=new AIPlayer(game->getPD(3,0),game->getPD(3,1),game->getPD(3,2),yellow,*game,p1);
			p1->setOpponent(p2);
		}else{
			p1=new HPlayer(game->getPD(0,0),game->getPD(0,1),game->getPD(0,2),blue,*game,CTRL_WASD);
			if(numPlayers>=2)
			{
				p2=new HPlayer(game->getPD(1,0),game->getPD(1,1),game->getPD(1,2),red,*game,CTRL_ARROWS);
			}else{
				p2=new AIPlayer(game->getPD(1,0),game->getPD(1,1),game->getPD(1,2),red,*game,p1);
			}
			if(numPlayers>=3)
			{
				p3=new HPlayer(game->getPD(2,0),game->getPD(2,1),game->getPD(2,2),green,*game,CTRL_IJKL);
			}else{
				p3=&unPlayer;
			}
			if(numPlayers==4)
			{
				p4=new HPlayer(game->getPD(3,0),game->getPD(3,1),game->getPD(3,2),yellow,*game,CTRL_NUM);
			}else{
				p4=&unPlayer;
			}
		}
		bool quit=false;

//Press ESC to quit out of the loop and end the game
		while(!quit)
		{
			try
			{
				if(p1->isAlive()){
					p1->advance();
				}
				if(p2->isAlive()){
					p2->advance();
				}
				if(p3->isAlive()){
					p3->advance();
				}
				if(p4->isAlive()){
					p4->advance();
				}
				Sleep(SLEEPTIME);
			}
			catch(GMException v)
			{
				scrn.move(0,43);
				v.printEx(cout);
				cout<<"\nPlayer1 x:"<<p1->getX()<<"\tPlayer1 y:"<<p1->getY();
				cout<<"\nPlayer2 x:"<<p2->getX()<<"\tPlayer2 y:"<<p2->getY();
				quit=true;
			}

			//checks to see if the quit button is pressed or if one or fewer players is alive
			if(KEYDOWN(VK_ESCAPE)||(p1->isAlive()+p2->isAlive()+p3->isAlive()+p4->isAlive()<=1))
			{
				quit=true;
			}			
		}
		scrn.move(0,44);
		if(p1->isAlive()+p2->isAlive()+p3->isAlive()+p4->isAlive()>1)
		{
			scrn.setColor(white);
			cout<<"You quit!\n";
		}else{
			if(p1->isAlive()+p2->isAlive()+p3->isAlive()+p4->isAlive()==0)
			{
				scrn.setColor(white);
				cout<<"Draw!\n";
			}
			if(p1->isAlive())
			{
				scrn.setColor(p1->getColor());
				cout<<"Player 1 wins!\n";
				p1Wins++;
			}
			if(p2->isAlive())
			{
				scrn.setColor(p2->getColor());
				cout<<"Player 2 wins!\n";
				if(numPlayers>=2)
				{
					p2Wins++;
				}
			}
			if(p3->isAlive())
			{
				scrn.setColor(p3->getColor());
				cout<<"Player 3 wins!\n";
				if(numPlayers>=2)
				{
					p3Wins++;
				}
			}
			if(p4->isAlive())
			{
				scrn.setColor(p4->getColor());
				cout<<"Player 4 wins!\n";
				if(numPlayers>=2)
				{
					p4Wins++;
				}
			}
		}
		cout<<"Press enter to continue.";
		m.pause();
		delete p1;
		delete p2;
		if(p3!= &unPlayer)
		{
			delete p3;
		}
		if(p4!= &unPlayer)
		{
			delete p4;
		}
		delete game;
		scrn.clear();
		scrn.move(0,0);
		scrn.setColor(white);		
		cout<<"Play again(Y/N)?";
		bool yesno=false;
		m.playAgain(running,p1Wins,p2Wins,p3Wins,p4Wins);

	}
	return 0;
}
